﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_UserControl_ucActionList_All : System.Web.UI.UserControl
{
    TblJobNoteDAO jobNoteDAO = new TblJobNoteDAO(Global.CONNECTION_STRING);


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadNotesType();
        }
    }

    private void LoadNotesType()
    {
        grdJobList.DataSource = jobNoteDAO.GetJobNoteAll();
        grdJobList.DataBind();
    }
    protected void drStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow grd in grdJobList.Rows)
            {
                // update xong- insert vo TblJobNotes
                TblJobNotes jobnote = new TblJobNotes();
                DropDownList drStatus = (DropDownList)grd.FindControl("drStatus");
                HiddenField hidden = (HiddenField)grd.FindControl("hid");
                string t = hidden.Value;
                string value = drStatus.SelectedValue;
                jobNoteDAO.Update(Convert.ToInt32(value), t);


                //jobnote.JobID = Global.ConvertToInt(t);
                HiddenField hidJobId = (HiddenField)grd.FindControl("hidJob");
                string idJob = hidJobId.Value;

                jobnote.JobID = Global.ConvertToInt(idJob);
                jobnote.Notes = "Finish" + " .ACTION BY " + ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerFullname.Trim();
                jobnote.StatusJobNote = "Has been Actioned";
                jobnote.StatusJob = 1;
                jobnote.DateEntered = DateTime.Now;
                jobnote.NoteType = "Requires Action";

                jobNoteDAO.InsUp_JobNote(jobnote, 1);

                LoadNotesType();
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }
    protected void grdJobList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            HiddenField hidItemID = (HiddenField)e.Row.Cells[0].FindControl("hid");
            hidItemID.Value = DataBinder.Eval(e.Row.DataItem, "IDJobNote").ToString();
            string id = hidItemID.Value;

            HiddenField hidJobID = (HiddenField)e.Row.Cells[0].FindControl("hidJob");
            hidJobID.Value = DataBinder.Eval(e.Row.DataItem, "JobID").ToString();

            DropDownList drstatus1 = (DropDownList)e.Row.Cells[7].FindControl("drStatus");

            DataTable dt = jobNoteDAO.GetJobNoteID(id);

            foreach (DataRow dr in dt.Rows)
            {
                int status = Convert.ToInt32(dr["StatusJob"].ToString());
                if (status == 1)
                {
                    drstatus1.SelectedValue = "1";

                }
                else
                {
                    drstatus1.SelectedValue = "0";
                }
            }

        }
    }
}
